package com.hongxun.design.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.fragments.BitTypeTabFragment;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.titlepopupwindow.BitTypeBean;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.PopwindowUtil;

/**
 * Created by zerdoor_pc on 2016/5/6.
 * 点位设置-类型选择
 */
public class BitTypeActivity extends BaseActivity implements HeadSecond, View.OnClickListener {
    // 三个选项卡
    private LinearLayout tab1Layout, tab2Layout;
    // 默认选中第一个tab
    private int index = 2;
    // fragment管理类
    private FragmentManager fragmentManager;
    // 三个fragment
    private BitTypeTabFragment tab1Fragment, tab2Fragment;
    TextView location_word, char_word;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bittype);
        findBar();
        findView();
        initialization();
    }

    @Override
    protected void onStart() {
        MyApp.activity = this;
        super.onStart();
    }



    @Override
    public void findBar() {
        HeadSecondControlPanel bit_layout = (HeadSecondControlPanel) findViewById(R.id.bit_layout);
        bit_layout.initHeadPanel(DataUtil.getsecondbean("", View.VISIBLE, R.color.bar_right, ListenerSet.back, MString.BitType, R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }

    RelativeLayout bit_tab, bit_pop;
    TextView bittype_right;

    @Override
    protected void findView() {
        //点位类型选择
        bit_pop = (RelativeLayout) findViewById(R.id.bit_pop);
        bittype_right = (TextView) findViewById(R.id.bittype_right);
        //tab
        bit_tab = (RelativeLayout) findViewById(R.id.bit_tab);

        super.findView();
    }

    @Override
    protected void initialization() {
        setTypeChoice();
        init();
        super.initialization();
    }

    /**
     * 设置点位类型*
     */
    private void setTypeChoice() {
        SetTypeface.set(bittype_right, IconSourse.DROPDOWN);
        bit_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyLog.L(TAG + "点击类型选择器");
                PopwindowUtil.openBitPop(view, bit_tab, mActivity);
            }
        });
    }




    /**
     * 初始化控件
     */
    private void init() {
        fragmentManager = getSupportFragmentManager();
        tab1Layout = (LinearLayout) findViewById(R.id.location_point);
        tab2Layout = (LinearLayout) findViewById(R.id.char_point);
        location_word = (TextView) findViewById(R.id.location_point_word);
        char_word = (TextView) findViewById(R.id.char_point_word);

        tab1Layout.setOnClickListener(this);
        tab2Layout.setOnClickListener(this);
//        setDefaultFragment();

        checkChange(tab1Fragment, tab1Layout, location_word, 1);


        TextView bittype_icon = (TextView) findViewById(R.id.bittype_icon);
        TextView bittype_title = (TextView) findViewById(R.id.bittype_title);
        BitTypeBean  bean=new BitTypeBean("新", "新建点");
        MyApp.currExploration.setPoint_type("11");
        bittype_icon.setText(bean.getType());
        bittype_title.setText(bean.getName());
    }


    /**
     * 切换fragment
     *
     * @param newFragment
     */
    private void replaceFragment(Fragment newFragment, int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (!newFragment.isAdded()) {
            Bundle bundle = new Bundle();
            bundle.putInt(MString.TYPE, i);
            newFragment.setArguments(bundle);
            transaction.replace(R.id.content_layout, newFragment);
            transaction.commit();
        } else {
            transaction.show(newFragment);
        }
    }

    /**
     * 改变现象卡的选中状态
     */
    private void clearStatus() {
        if (index == 1) {
            tab1Layout.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            location_word.setTextColor(getResources().getColor(R.color.title));
        } else if (index == 2) {
            tab2Layout.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            char_word.setTextColor(getResources().getColor(R.color.title));
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.location_point:
                checkChange(tab1Fragment, tab1Layout, location_word, 1);
                break;
            case R.id.char_point:
                checkChange(tab2Fragment, tab2Layout, char_word, 2);
                break;
        }


    }

    private void checkChange(BitTypeTabFragment f, View v, TextView words, int i) {
        clearStatus();
        if (f == null) {
            f = new BitTypeTabFragment();
        }
        replaceFragment(f, i);
//        v.setBackgroundColor(getResources().getColor(
//                R.color.white));
        v.setBackgroundResource(R.drawable.grayline_wihte_tabitem);
        words.setTextColor(getResources().getColor(R.color.caikan_bule));
        index = i;

    }

    @Override
    protected void onDestroy() {
        tab1Fragment=null;
        tab2Fragment=null;

        super.onDestroy();

    }
}
